import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public class CinemaSystem {
    private final UserService userService;
    private final SalesService salesService;
    private List<Hall> halls;

    public CinemaSystem() {
        userService = new UserService();
        salesService = new SalesService();
        halls = new ArrayList<>();
    }

    public void addHall(Hall hall) {
        halls.add(hall);
    }

    public List<Hall> getHalls() {
        return halls;
    }

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        User loginUser = null;
        while (true) {
            System.out.println("===== 影院管理系统登录 =====");
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine();
            System.out.print("请输入密码: ");
            String password = scanner.nextLine();
            loginUser = userService.login(username, password);
            if (loginUser != null) {
                System.out.println("登录成功，欢迎 " + loginUser.getUsername() + "！");
                break;
            } else {
                System.out.println("用户名或密码错误，请重试。\n");
            }
        }

        switch (loginUser.getRole()) {
            case "admin":
                Admin admin = (Admin) loginUser;
                while (true) {
                    System.out.println("\n===== 管理员菜单 =====");
                    System.out.println("1. 用户管理");
                    System.out.println("2. 查看操作日志");
                    System.out.println("0. 退出");
                    System.out.print("请选择: ");
                    String choice = scanner.nextLine();
                    switch (choice) {
                        case "1": admin.manageUsers(userService, scanner); break;
                        case "2": admin.viewLogs(); break;
                        case "0": System.out.println("已退出系统。"); return;
                        default: System.out.println("无效选择，请重试。");
                    }
                }
            case "manager":
                Manager manager = (Manager) loginUser;
                while (true) {
                    System.out.println("\n===== 经理菜单 =====");
                    System.out.println("1. 影片管理");
                    System.out.println("2. 排片管理");
                    System.out.println("3. 销售统计");
                    System.out.println("0. 退出");
                    System.out.print("请选择: ");
                    String choice = scanner.nextLine();
                    switch (choice) {
                        case "1": manager.manageMovies(scanner); break;
                        case "2": manager.manageSchedules(scanner); break;
                        case "3": manager.viewSales(salesService, scanner); break;
                        case "0": System.out.println("已退出系统。"); return;
                        default: System.out.println("无效选择，请重试。");
                    }
                }
            case "clerk":
                Clerk clerk = (Clerk) loginUser;
                while (true) {
                    System.out.println("\n===== 前台菜单 =====");
                    System.out.println("1. 列出所有影片");
                    System.out.println("2. 列出所有场次");
                    System.out.println("3. 查询场次座位");
                    System.out.println("4. 售票");
                    System.out.println("5. 退票");
                    System.out.println("0. 退出");
                    System.out.print("请选择: ");
                    String choice = scanner.nextLine();
                    switch (choice) {
                        case "1": clerk.showMovies(); break;
                        case "2": clerk.showSchedules(); break;
                        case "3": clerk.showSeats(scanner); break;
                        case "4": clerk.sellTicket(scanner); break;
                        case "5": clerk.refundTicket(scanner); break;
                        case "0": System.out.println("已退出系统。"); return;
                        default: System.out.println("无效选择，请重试。");
                    }
                }
            default:
                System.out.println("未知用户类型，退出。");
        }
    }
} 